﻿using STF.Blog.Common;
using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class BlogService : IBlogService
    {

        private IBlogManager _ib;
        public BlogService(IBlogManager ib)
        {
            _ib = ib;
        }

        public async Task<int> AddBlogAsync(string title, string content, DateTime publishTime, int stateCode, int like, int trample, int typeId, int fbxsId, int liveId, int uid, int coverId)
        {
            return await _ib.AddAsync(new Model.Blog 
            {
                Title = title,
                Content = content,
                PublishTime = publishTime,
                StateCode = (Common.StateCode)stateCode,
                Like = like,
                Trample = trample,
                TypeId = typeId,
                FbxsId = fbxsId,
                LevelId = liveId,
                Uid = uid,
                CoverId = coverId
            });
        }

        public async Task<int> DeleteBlogAsync(int id)
        {
            var data = await _ib.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ib.DeleteAsync(data);
        }

        public async Task<int> GetByBidAsync(int userId)
        {
            var data = await _ib.GetChooseAsync(b => b.Uid == userId);
            var articleId = data.OrderByDescending(b => b.CreateTime).First();
            return articleId.Id;
        }
        public async Task<List<BlogDto>> GetAllBlogAsync(int deleteId = 1, int stateCode = 2)
        {
            var data = await _ib.GetChooseAsync(b => b.DeleteId == deleteId && b.StateCode == (StateCode)stateCode);
            return data == null ? new List<BlogDto>() : data.Select(b => new BlogDto
            {
                Id = b.Id,
                Title = b.Title,
                Content = b.Content,
                PublishTime = b.PublishTime,
                StateCode = b.StateCode,
                Like = b.Like,
                Trample = b.Trample,
                TypeId = b.TypeId,
                FbxsId = b.FbxsId,
                LevelId = b.LevelId,
                Uid = b.Uid,
                CoverId = b.CoverId,
                CreateTime = b.CreateTime,
                UpdateTime = b.UpdateTime
            }).ToList();
        }

        public async Task<List<BlogDto>> GetByDateAsync(int deleteId = 1, int stateCode = 2)
        {
            var data = await _ib.GetChooseAsync(bg => bg.DeleteId == deleteId && bg.StateCode == (StateCode)stateCode);
            return data == null ? new List<BlogDto>() : data.Select(b => new BlogDto 
            {
                Id = b.Id,
                Title = b.Title,
                Content = b.Content,
                PublishTime = b.PublishTime,
                StateCode = b.StateCode,
                Like = b.Like,
                Trample = b.Trample,
                TypeId = b.TypeId,
                FbxsId = b.FbxsId,
                LevelId = b.LevelId,
                Uid = b.Uid,
                CoverId= b.CoverId,
                CreateTime = b.CreateTime,
                UpdateTime = b.UpdateTime
            }).OrderByDescending(b => b.CreateTime).Take(6).ToList();
        }

        public async Task<List<BlogDto>> GetByLikeAsync(int deleteId = 1, int stateCode = 2)
        {
            var data = await _ib.GetChooseAsync(bg => bg.DeleteId == deleteId && bg.StateCode == (StateCode)stateCode);
            return data == null ? new List<BlogDto>() : data.Select(b => new BlogDto
            {
                Id = b.Id,
                Title = b.Title,
                Content = b.Content,
                PublishTime = b.PublishTime,
                StateCode = b.StateCode,
                Like = b.Like,
                Trample = b.Trample,
                TypeId = b.TypeId,
                FbxsId = b.FbxsId,
                LevelId = b.LevelId,
                Uid = b.Uid,
                CoverId = b.CoverId,
                CreateTime = b.CreateTime,
                UpdateTime = b.UpdateTime
            }).OrderByDescending(b => b.Like).Take(6).ToList();
        }

        public async Task<BlogDto> GetByIdAsync(int id)
        {
            var data = await _ib.GetById(id);
            return data == null ? new BlogDto() : new BlogDto 
            {
                Id = data.Id,
                Title = data.Title,
                Content = data.Content,
                PublishTime = data.PublishTime,
                StateCode = data.StateCode,
                Like = data.Like,
                Trample = data.Trample,
                TypeId = data.TypeId,
                FbxsId = data.FbxsId,
                LevelId = data.LevelId,
                Uid = data.Uid,
                CoverId = data.CoverId,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<BlogDto>> GetByNameAsync(string title, int deleteId = 1, int stateCode = 1)
        {
            var data = await _ib.GetChooseAsync(b => b.Title.Contains(title) && b.DeleteId == deleteId && b.StateCode == (StateCode)stateCode);
            return data == null ? new List<BlogDto>() : data.Select(b => new BlogDto 
            {
                Id = b.Id,
                Title = b.Title,
                Content = b.Content,
                PublishTime = b.PublishTime,
                StateCode = b.StateCode,
                Like = b.Like,
                Trample = b.Trample,
                TypeId = b.TypeId,
                FbxsId = b.FbxsId,
                LevelId = b.LevelId,
                Uid = b.Uid,
                CoverId = b.CoverId,
                CreateTime = b.CreateTime,
                UpdateTime = b.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ib.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ib.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ib.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ib.UpdateAsync(data);
        }

        public async Task<int> UpdateBlogAsync(int id, string title, string content, DateTime publishTime, int stateCode, int like, int trample, int typeId, int fbxsId, int leveId, int uid, int coverId)
        {
            var data = await _ib.GetById(id);
            if(data == null)
                return 0;
            data.Title = title;
            data.Content = content;
            data.PublishTime = publishTime;
            data.StateCode = (Common.StateCode)stateCode;
            data.Like = like;
            data.Trample = trample;
            data.TypeId = typeId;
            data.FbxsId= fbxsId;
            data.LevelId=leveId;
            data.Uid = uid;
            data.UpdateTime = DateTime.Now;
            return await _ib.UpdateAsync(data);
        }

        public async Task<int> UpdateStateCode(int stateId, int stateCode)
        {
            var data = await _ib.GetById(stateId);
            if(data == null)
                return -1;
            data.StateCode = (StateCode)stateCode;
            return await _ib.UpdateAsync(data);
        }

        public async Task<List<BlogDto>> GetByIdListAsync(int id, int deleteId = 1, int stateCode = 2)
        {
            var data = await _ib.GetChooseAsync(b => b.Uid == id && b.DeleteId == deleteId && b.StateCode == (StateCode)stateCode);
            return data == null ? new List<BlogDto>() : data.Select(b => new BlogDto 
            {
                Id = b.Id,
                Title = b.Title,
                Content = b.Content,
                PublishTime = b.PublishTime,
                StateCode = b.StateCode,
                Like = b.Like,
                Trample = b.Trample,
                TypeId = b.TypeId,
                FbxsId = b.FbxsId,
                LevelId = b.LevelId,
                Uid = b.Uid,
                CoverId = b.CoverId,
                CreateTime = b.CreateTime,
                UpdateTime = b.UpdateTime
            }).ToList();
        }

        public async Task<List<BlogDto>> GetByUidListAsync(int uid)
        {
            var data = await _ib.GetChooseAsync(b => b.Uid == uid && b.DeleteId == 1 && b.StateCode == (StateCode)2);
            return data == null ? new List<BlogDto>() : data.Select(b => new BlogDto
            {
                Id = b.Id,
                Title= b.Title,
                Content= b.Content,
                PublishTime = b.PublishTime,
                StateCode = b.StateCode,
                Like = b.Like,
                Trample = b.Trample,
                TypeId = b.TypeId,
                FbxsId = b.FbxsId,
                LevelId = b.LevelId,
                Uid = b.Uid,
                CoverId = b.CoverId,
                CreateTime = b.CreateTime,
                UpdateTime = b.UpdateTime
            }).ToList();
        }
    }
}
